home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / util / showmount / showmount.c next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  5.3 KB  |  256 lines

  1. /*
  2.  * $Id: showmount.c,v 1.3 1994/05/15 18:32:56 jraja Exp $
  3.  *
  4.  * showmount.c -- show mount information for an NFS server
  5.  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2, or (at your option)
  10.  * any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <rpc/rpc.h>
  20. #include <sys/socket.h>
  21. #include <sys/time.h>
  22. #include <string.h>
  23. #include <netdb.h>
  24. #include <arpa/inet.h>
  25. #include <errno.h>
  26.  
  27. #ifndef amigados
  28. #include <getopt.h>
  29. #endif
  30.  
  31. #ifdef __STDC__
  32. #include <stdlib.h>
  33.  
  34. int dump_cmp(char ** p, char ** q);
  35. int main(int argc, char ** argv);
  36. #endif
  37.  
  38. #include "mount.h"
  39.  
  40. #include "showmount_rev.h"
  41. const char version[] = VERSTAG;
  42.  
  43. int headers = 1;
  44. int hflag = 0;
  45. int aflag = 0;
  46. int dflag = 0;
  47. int eflag = 0;
  48. char *progname;
  49.  
  50. int dump_cmp(p, q)
  51. char **p;
  52. char **q;
  53. {
  54.     return strcmp(*p, *q);
  55. }
  56.  
  57. int main(argc, argv)
  58. int argc;
  59. char **argv;
  60. {
  61.     char *hostname;
  62.     enum clnt_stat clnt_stat;
  63.     struct hostent *hp;
  64.     struct sockaddr_in server_addr;
  65.     int msock;
  66.     struct timeval total_timeout;
  67.     struct timeval pertry_timeout;
  68.     int c;
  69.     CLIENT *mclient;
  70.     groups grouplist;
  71.     exports exportlist;
  72.     mountlist dumplist;
  73.     mountlist list;
  74.     int i;
  75.     int n;
  76.     char **dumpv;
  77.  
  78.     memset(&grouplist, 0, sizeof(grouplist));
  79.     memset(&exportlist, 0, sizeof(exportlist));
  80.     memset(&dumplist, 0, sizeof(dumplist));
  81.     memset(&list, 0, sizeof(list));
  82.  
  83.     progname = argv[0];
  84.     while ((c = getopt(argc, argv, "adeh")) != EOF) {
  85.         switch (c) {
  86.         case 'a':
  87.             aflag = 1;
  88.             break;
  89.         case 'd':
  90.             dflag = 1;
  91.             break;
  92.         case 'e':
  93.             eflag = 1;
  94.             break;
  95.         case 'h':
  96.             headers = 0;
  97.             break;
  98.         default:
  99.             fprintf(stderr, "usage: %s [-ade] [host]\n", progname);
  100.             exit(1);
  101.             break;
  102.         }
  103.     }
  104.     argc -= optind;
  105.     argv += optind;
  106.  
  107.     switch (aflag + dflag + eflag) {
  108.     case 0:
  109.         hflag = 1;
  110.         break;
  111.     case 1:
  112.         break;
  113.     default:
  114.         fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
  115.             progname);
  116.         exit(1);
  117.         break;
  118.     }
  119.  
  120.     switch (argc) {
  121.     case 0:
  122.         hostname = "localhost";
  123.         break;
  124.     case 1:
  125.         hostname = argv[0];
  126.         break;
  127.     default:
  128.         fprintf(stderr, "%s: only one hostname is allowed\n",
  129.             progname);
  130.         exit(1);
  131.         break;
  132.     }
  133.  
  134.     if (hostname[0] >= '0' && hostname[0] <= '9') {
  135.         server_addr.sin_family = AF_INET;
  136.         server_addr.sin_addr.s_addr = inet_addr(hostname);
  137.     }
  138.     else {
  139.         if ((hp = gethostbyname(hostname)) == NULL) {
  140.             fprintf(stderr, "%s: can't get address for %s\n",
  141.                 progname, hostname);
  142.             exit(1);
  143.         }
  144.         server_addr.sin_family = AF_INET;
  145.         memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
  146.     }
  147.  
  148.     /* create mount deamon client */
  149.  
  150.     server_addr.sin_port = 0;
  151.     msock = RPC_ANYSOCK;
  152.     if ((mclient = clnttcp_create(&server_addr,
  153.         MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) {
  154.         server_addr.sin_port = 0;
  155.         msock = RPC_ANYSOCK;
  156.         pertry_timeout.tv_sec = 3;
  157.         pertry_timeout.tv_usec = 0;
  158.         if ((mclient = clntudp_create(&server_addr,
  159.             MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
  160.             clnt_pcreateerror("mount clntudp_create");
  161.             exit(1);
  162.         }
  163.     }
  164.     mclient->cl_auth = authunix_create_default();
  165.     total_timeout.tv_sec = 20;
  166.     total_timeout.tv_usec = 0;
  167.  
  168.     if (eflag) {
  169.         clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
  170.             xdr_void, NULL, xdr_exports, &exportlist,
  171.             total_timeout);
  172.         if (clnt_stat != RPC_SUCCESS) {
  173.             clnt_perror(mclient, "rpc mount export");
  174.             exit(1);
  175.         }
  176.         printf("Exports list on %s:\n", hostname);
  177.         while (exportlist) {
  178.             printf("%-21s ", exportlist->ex_dir);
  179.             grouplist = exportlist->ex_groups;
  180.             if(!grouplist)
  181.                 printf("\n");
  182.             while (grouplist) {
  183.                 printf("%s%s", grouplist->gr_name,
  184.                     grouplist->gr_next ? "," : "\n");
  185.                  grouplist = grouplist->gr_next;
  186.             }
  187.             exportlist = exportlist->ex_next;
  188.         }
  189.         exit(0);
  190.     }
  191.  
  192.     clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
  193.         xdr_void, NULL, xdr_mountlist, &dumplist,
  194.         total_timeout);
  195.     if (clnt_stat != RPC_SUCCESS) {
  196.         clnt_perror(mclient, "rpc mount dump");
  197.         exit(1);
  198.     }
  199.  
  200.     n = 0;
  201.     for (list = dumplist; list; list = list->ml_next)
  202.         n++;
  203.     dumpv = (char **) calloc(n, sizeof (char *));
  204.     if (n && !dumpv) {
  205.         fprintf(stderr, "%s: out of memory\n", progname);
  206.         exit(1);
  207.     }
  208.     i = 0;
  209.  
  210.     if (hflag) {
  211.         if (headers)
  212.             printf("Hosts on %s:\n", hostname);
  213.         while (dumplist) {
  214.             dumpv[i++] = dumplist->ml_hostname;
  215.             dumplist = dumplist->ml_next;
  216.         }
  217.     }
  218.     else if (aflag) {
  219.         if (headers)
  220.             printf("All mount points on %s:\n", hostname);
  221.         while (dumplist) {
  222.             char s[1024];
  223.             char *t;
  224.  
  225.             sprintf(s, "%s:%s", dumplist->ml_hostname,
  226.                 dumplist->ml_directory);
  227.             t = malloc(strlen(s) + 1);
  228.             if (t)
  229.                 strcpy(t, s);
  230.             else {
  231.                 printf("%s: out of memory\n", progname);
  232.                 exit(1);
  233.             }
  234.             dumpv[i++] = t;
  235.             dumplist = dumplist->ml_next;
  236.         }
  237.     }
  238.     else if (dflag) {
  239.         if (headers)
  240.             printf("Directories on %s:\n", hostname);
  241.         while (dumplist) {
  242.             dumpv[i++] = dumplist->ml_directory;
  243.             dumplist = dumplist->ml_next;
  244.         }
  245.     }
  246.  
  247.     qsort(dumpv, n, sizeof (char *), dump_cmp);
  248.     
  249.     for (i = 0; i < n; i++) {
  250.         if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
  251.             printf("%s\n", dumpv[i]);
  252.     }
  253.     exit(0);
  254. }
  255.  
  256.